package org.jupiter.blog.authentic.service;

import com.google.common.collect.Lists;
import org.jupiter.blog.api.domain.JupiterUser;
import org.jupiter.blog.service.mapper.JupiterUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class JupiterUserDetailsService implements UserDetailsService {

	@Autowired
	private JupiterUserMapper userMapper;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		JupiterUser user = userMapper.selectByUsernameOrEmail(username, username);
		if (user == null){
			throw new UsernameNotFoundException("用户不存在: " + username);
		}
		return User
				.withUsername(username)
				.password(user.getPasswordHash())
				.authorities(Lists.newArrayList())
				.accountLocked(user.getIsLocked())
				.disabled(!user.getIsActive())
				//TODO
				.accountExpired(false)
				.build();
	}
}


